<?php
class AdminUserController extends Zendvn_Controller_Action{
	protected  $_arrParam;
	protected  $_currentController;
	protected  $_actionMain;
	protected  $_namespace;
	protected $_paginator = array(
									'itemCountPerPage' => 5,
									'pageRange' => 3,
								  ); 
	
	public function init(){
		$this->_arrParam 			= $this->_request->getParams();
		
		$this->_currentController 	= '/'    . $this->_arrParam['module']
								     .'/'  . $this->_arrParam['controller'];
		
		$this->_actionMain 		   = '/'    . $this->_arrParam['module']
								     .'/'  . $this->_arrParam['controller']
								     .'/index';
								     
		$this->_paginator['currentPage'] = $this->_request->getParam('page',1);
		$this->_arrParam['paginator'] = $this->_paginator;
		
		//Tao namspace cho session
		$this->_namespace = $this->_arrParam['module'] . '-' . $this->_arrParam['controller'];
		$ssFilter = new Zend_Session_Namespace($this->_namespace);
		//$ssFilter->unsetAll();
		if(empty($ssFilter->keywords)){
			$ssFilter->keywords = '';
		}
		if(empty($ssFilter->colum)){
			$ssFilter->colum    = 'u.id';
			$ssFilter->by		='ASC';
			$ssFilter->slbgroup = 0;
		}
		$this->_arrParam['ssFilter']['keywords'] = $ssFilter->keywords;
		$this->_arrParam['ssFilter']['colum'] 	 = $ssFilter->colum;
		$this->_arrParam['ssFilter']['by']		 = $ssFilter->by;
		$this->_arrParam['ssFilter']['slbgroup'] = $ssFilter->slbgroup;
		
		
		
		//Truyen ra ngoai view
		$this->view->arrParam 			= $this->_arrParam;
		$this->view->currentController 	= $this->_currentController;
		$this->view->actionMain  		= $this->_actionMain;
		$this->view->namespace			= $this->_namespace;
		//Khai bao template		
		$template_path = TEMPLATE_PATH . "/admin/system";
		$this->loadTemplate($template_path,'template.ini','template');
	}
	public function indexAction(){
		
		$this->view->Title = 'Thành viên :: Quản lý thành viên :: Danh sách';
		$this->view->headTitle($this->view->Title,true);
		
		$tblUser = new Default_Model_Users();
		$this->view->Items = $tblUser->listItem($this->_arrParam, array('task'=>'admin-list'));
		$this->view->SelectGroup = $tblUser->itemInSelectbox();
		
		
		//Xay dung phan trang
		$totalItem = $tblUser->countItem($this->_arrParam);
		$paginator = new Zendvn_Paginator();
		$this->view->paginator = $paginator->createPaginator($totalItem,$this->_paginator);
		
	}
	public function filterAction(){
		
		$ssFilter = new Zend_Session_Namespace($this->_namespace);
		if($this->_arrParam['type'] == 'search'){
			if($this->_arrParam['key']== 1){
				$ssFilter->keywords = trim($this->_arrParam['keywords']);
			}else{
				$ssFilter->keywords = '';
			}
		}
		if($this->_arrParam['type'] == 'order'){
			$ssFilter->colum 	= $this->_arrParam['colum'];
			$ssFilter->by 		= $this->_arrParam['by'];
		}
		if($this->_arrParam['type'] == 'slbgroup'){
			$ssFilter->slbgroup = $this->_arrParam['formSelect'];
		}
		$this->_redirect($this->_actionMain);
		$this->_helper->viewRenderer->setNoRender();
	}
	public function addAction(){
		$this->view->Title = 'Thành viên :: Quản lý thành viên :: Thêm mới';
		$this->view->headTitle($this->view->Title,true);
		
		$tblUser = new Default_Model_Users();
		$this->view->SelectGroup = $tblUser->itemInSelectbox();
		
		if($this->_request->isPost()){
			$validator = new Default_Form_Validate_Users_Addnew($this->_arrParam);
			if($validator->isError() == true){
				$this->view->arrErrors = $validator->getMessageError();
				$this->view->Item	   = $validator->getData();
			}else{
				$tblUser = new Default_Model_Users();
				$arrParams = $validator->getData('upload');
				$tblUser->saveItem($arrParams,array('task'=>'admin-add'));
				$this->_redirect($this->_actionMain);
			}
		}
	}
	public function infoAction(){
		$this->view->Title = 'Thành viên :: Quản lý thành viên :: Thông tin';
		$this->view->headTitle($this->view->Title,true);
		$tblGroup = new Default_Model_Users();
		$this->view->Item = $tblGroup->getItem($this->_arrParam,array('task'=>'admin-info'));
		
	}
	public function editAction(){
		$this->view->Title = 'Thành viên :: Quản lý thành viên :: Sửa';
		$this->view->headTitle($this->view->Title,true);
		$tblUser = new Default_Model_Users();
		$this->view->flag = true;
		$this->view->Item = $tblUser->getItem($this->_arrParam,array('task'=>'admin-edit'));
		$this->view->SelectGroup = $tblUser->itemInSelectbox();
		if($this->_request->isPost()){
			$validator = new Default_Form_Validate_Users_Edit($this->_arrParam);
			if($validator->isError() == true){
				$this->view->flag = false;
				$this->view->arrErrors = $validator->getMessageError();
				$newArr	    = $validator->getData();
				$newArrItem = $newArr;
				if(!empty($newArr['current_user_avatar'])){
					$newArrItem['avt']    	   = $newArr['current_user_avatar'];
					$newArrItem['user_avatar'] = $newArr['current_user_avatar'];
				}
				$this->view->Item = $newArrItem;
			}
			else{
				$this->view->flag = true;
				$arrParams = $validator->getData('upload');
				$tblUser = new Default_Model_Users();
				$tblUser->saveItem($arrParams,array('task'=>'admin-edit'));
				$this->_redirect($this->_actionMain);
			}
		}
	}
	public function deleteAction(){
		$this->view->Title = 'Thành viên :: Quản lý thành viên :: Xóa';
		$this->view->headTitle($this->view->Title,true);
		
		$this->view->Item = $this->_arrParam;
		if($this->_request->isPost()){
			$tblUser = new Default_Model_Users();
			$tblUser->deleteItem($this->_arrParam,array('task'=>'admin-delete'));
			$this->_redirect($this->_actionMain);
		}
	}
	public function statusAction(){
			$tblUser = new Default_Model_Users();
			$tblUser->changeStatus($this->_arrParam,array('task'=>'admin-status'));
			$this->_redirect($this->_actionMain);
			$this->_helper->viewRenderer->setNoRender();
	}

	public function multiDeleteAction(){
		$this->_helper->viewRenderer->setNoRender();
		if($this->_request->isPost()){
			$tblUser = new Default_Model_Users();
			$tblUser->deleteItem($this->_arrParam,array('task'=>'admin-multidelete'));
			$this->_redirect($this->_actionMain);
			
		}
	}

}













